/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.personal.service.impl;

import com.koron.common.bean.StaffBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.personal.domain.UserWorkbench;
import com.koron.personal.mapper.UserWorkbenchMapper;
import com.koron.personal.service.api.UserWorkbenchService;
import com.koron.personal.utils.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.Date;
import java.util.List;

/**
 * @Author: liaodf
 * @Date: 2021/5/24 19:52
 */
@Service
public class UserWorkbenchServiceImpl implements UserWorkbenchService {

    private final static  Logger logger = LoggerFactory.getLogger(UserWorkbenchServiceImpl.class);

    @Override
    public MessageBean<Integer> save(StaffBean user, List<UserWorkbench> userWorkbenchList) {
        MessageBean<Integer> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Integer.class);
        SessionFactory factory = null;
        try {
            factory = new SessionFactory();
            UserWorkbenchMapper mapper = factory.getMapper(UserWorkbenchMapper.class);
            // 删除用户已有的模块权限
            mapper.delete(user.getCode());
            if (null != userWorkbenchList && userWorkbenchList.size() > 0) {
                for (UserWorkbench bean : userWorkbenchList) {
                    MessageBean messageBean = check(bean);
                    if (messageBean.getCode() != BusinessCode.SUCCESS.getCode()) {
                        return messageBean;
                    }
                    bean.setId(DefaultIdGenerator.getInstance().generateLongId());
                    bean.setAddTime(new Date());
                    bean.setUserCode(user.getCode());
                    bean.setUserName(user.getName());
                }
                //添加新保存的模块权限
                mapper.insert(userWorkbenchList);
            }
            factory.close();
            return msg;
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            if (null != factory) {
                factory.close(false);
            }
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription("操作失败");
            return msg;
        } finally {
            if (null != factory) {
                factory.close();
            }
        }
    }

    private MessageBean check(UserWorkbench bean) {
        if (StringUtils.isEmpty(bean.getCode()) || StringUtils.isEmpty(bean.getName()) || StringUtils.isEmpty(bean.getUrl())) {
            return MessageBean.create(BusinessCode.FAILURE.getCode(), "必要参数不能为空", void.class);
        }
        return MessageBean.create(BusinessCode.SUCCESS.getCode(), "验证无误", void.class);
    }

    @Override
    public MessageBean<List> list(StaffBean user) {
        MessageBean<List> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        try (SessionFactory factory = new SessionFactory()) {
            UserWorkbenchMapper mapper = factory.getMapper(UserWorkbenchMapper.class);
            // 查询用户的工作台信息
            List<UserWorkbench> userWorkbenchList = mapper.list(user.getCode());
            msg.setData(userWorkbenchList);
            factory.close();
            return msg;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
            return msg;
        }
    }
}
